using Light.Assets.Scripts.EventManager;
using Refresh;
using UnityEngine;
using UnityEngine.UI;

namespace Light
{
    public class Skill : MonoBehaviour, IRefresh
    {
        [SerializeField] private Text _skillName;
        [SerializeField] private Image _skillImage;
        [SerializeField] private Button _button;
        private SkillItemSO data;


        private SkillsController parentPanel;

        private void Awake()
        {
            EventManager.Register<ChangeColorEvent>(ChangeSkillImageColor);
            _button = GetComponent<Button>();
        }

        private void OnEnable()
        {
            _button.onClick.AddListener(OnClickChangeActiveSkill);
        }

        private void OnDisable()
        {
            _button.onClick.RemoveAllListeners();
        }

        public void Refresh<T>(T data) where T : RefreshData<T>, new()
        {
            var info = data as ItemRefreshData;
            if (!(info.type == FunctionBarOperationType.Delete))
            {
                this.data = info.data as SkillItemSO;
                _skillName.text = this.data.itemName;
                _skillImage.sprite = this.data.image;
                if (this.data.isActive)
                    _skillImage.color = Color.white;
                else
                    _skillImage.color = Color.gray;
            }
        }

        public void Init(SkillsController panel)
        {
            parentPanel = panel;
        }

        public void OnClickChangeActiveSkill()
        {
            var info = ItemDetailRefreshData.Get();

            info.data = data;

            parentPanel.skillChangeUI.Refresh(info);
        }

        public void ChangeSkillImageColor(ChangeColorEvent skll)
        {
            if (data.isActive)
                _skillImage.color = Color.white;

            skll.Dispose();
        }
    }

    public class ChangeColorEvent : GameEvent<ChangeColorEvent>
    {
        public SkillItemSO skill;
    }
}